#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 110;

string s[N];
int n;

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n) cin >> s[i];
	int ans = 1e9;
	rep(x, 0, 9) {
		vec pot;
		rep(i, 1, n) {
			rep(j, 0, 9) if(s[i][j] == '0' + x) pot.eb(j);
		}
		sort(pot.begin(), pot.end());
		int cur = -1, lst = -1, ret = 0;
		for(auto v : pot) {
			if(lst != v) cur = -1, lst = -1;
			if(lst == -1) {
				lst = v; cur = v; chkmax(ret, v + 1);
			} else cur += 10, chkmax(ret, cur + 1);
		} chkmin(ans, ret);
	} cout << ans - 1 << endl;
	return 0;
}
